///引入基础库
import 'dart:async';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
///引入插件包
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:path_provider/path_provider.dart';
import 'package:catcher/catcher.dart';

///引入stroe
import './store/main.dart';

///引入视图
import 'pages/splash/splash_screen.dart';
import './router/routes.dart';

import './config/config.dart';

void main() async{
	LogUtil.init(tag: "来自LogUtil", isDebug: kDebugMode,maxLen:800);
	WidgetsFlutterBinding.ensureInitialized();
	Directory? externalDir;
	if (Platform.isAndroid) {
		externalDir = await getExternalStorageDirectory();
	}
	if (Platform.isIOS || Platform.isMacOS) {
		externalDir = await getApplicationDocumentsDirectory();
	}
	String path = "";
	if (externalDir != null) {
		path = externalDir.path.toString();
	}
	CatcherOptions debugOptions =CatcherOptions(SilentReportMode(), [
		ConsoleHandler(),
	],screenshotsPath:path);

	/// Release configuration. Same as above, but once user accepts dialog, user will be prompted to send email with crash to support.
	CatcherOptions releaseOptions = CatcherOptions(SilentReportMode(), [
		EmailAutoHandler(EmailConfig.host,EmailConfig.port,EmailConfig.from,EmailConfig.userName,EmailConfig.password,[EmailConfig.to])
	]);
	/// STEP 2. Pass your root widget (MyApp) along with Catcher configuration:
	Catcher(rootWidget: const MyApp(), debugConfig: debugOptions, releaseConfig: releaseOptions);
  	// runApp(const MyApp());
}

class MyApp extends StatelessWidget {
	const MyApp({Key? key}) : super(key: key);

	@override
	Widget build(BuildContext context) {
		return ScreenUtilInit(
			designSize: const Size(375, 375),
			builder: () {
				Get.put(MainStore());
				return GetBuilder<MainStore>(
					builder: (store){
						return Theme(
                            data: store.currentTheme,
                            child: GetCupertinoApp(
                                debugShowCheckedModeBanner: false,
								navigatorKey:Catcher.navigatorKey,
                                defaultTransition: Transition.cupertino,
                                transitionDuration: const Duration(milliseconds: 350),
                                localizationsDelegates: const [
                                    GlobalMaterialLocalizations.delegate,
                                    GlobalWidgetsLocalizations.delegate,
                                    GlobalCupertinoLocalizations.delegate,
                                ],
                                supportedLocales: const [
                                    Locale('zh'),
                                    Locale('en'),
                                ],
                                theme: const CupertinoThemeData(
                                    primaryColor:Colors.black,
                                ),
                                home: CatcherScreenshot(
									catcher: Catcher.getInstance(),
									child: const SplashScreen(),
								),
                                builder: (context,child){
                                    return FlutterSmartDialog(child: GestureDetector(
                                        onTap: () {
                                            FocusScopeNode currentFocus = FocusScope.of(context);
                                            if (!currentFocus.hasPrimaryFocus &&currentFocus.focusedChild != null) {
                                            	FocusManager.instance.primaryFocus?.unfocus();
                                            }
                                        },
                                        child: child,
                                    ));
                                },
								initialRoute: '/',
								getPages: routes,
                            ),
                        );
					}
				);
			},
		);
	}
}


